package com.example.zhaoping.controller;


import com.example.zhaoping.domain.ResponseResult;
import com.example.zhaoping.domain.User;
import com.example.zhaoping.dto.AddUserDto;
import com.example.zhaoping.dto.UserRegisterDto;
import com.example.zhaoping.service.IUserService;
import com.example.zhaoping.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czx
 * @since 2024-12-23
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    //http://127.0.0.1:8080/doc.html#/home

    @Autowired
    private IUserService userService;

    @ApiOperation("分页查询用户列表")
    @GetMapping("/list")
    public ResponseResult userList(Integer pageNum,Integer pageSize,String username){
        return userService.userList(pageNum, pageSize, username);
    }


    // 新增用户
    @ApiOperation("新增用户")
    @PostMapping("/adduser")
    public ResponseResult addUser(@RequestBody AddUserDto addUserDto) {
        User user = BeanCopyUtils.copyBean(addUserDto, User.class);
        userService.save(user);
        return ResponseResult.okResult();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/deleteuser/{id}")
    public ResponseResult deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        return ResponseResult.okResult();
    }

    @ApiOperation("修改用户")
    @PutMapping("/updateuser")
    public ResponseResult updateUser(@RequestBody User user)  {
        userService.updateById(user);
        return ResponseResult.okResult();
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/getuser/{id}")
    public ResponseResult getUser(@PathVariable Long id) {
        User user = userService.getById(id);
        return ResponseResult.okResult(user);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/deleteusers/{ids}")
    public ResponseResult deleteUsers(@PathVariable List<Long> ids) {
        userService.removeByIds(ids);
        return ResponseResult.okResult();
    }

    @ApiOperation("修改用户密码")
    @PutMapping("/resetpassword")
    public ResponseResult resetPassword(@RequestBody Map map) {
        ResponseResult responseResult = userService.resetPassword(map);
        return responseResult;

    }

    @ApiOperation("注册管理员用户")
    @PostMapping("/register")
    public ResponseResult register(@RequestBody @Validated UserRegisterDto userRegisterDto) {


        return  userService.userregister(userRegisterDto);
    }






}
